package com.mikasa.rabbitmq.api.consumer;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;

/**
 * @author mikasa
 */
public class Consumer {

	
	public static void main(String[] args) throws Exception {
		
		
		ConnectionFactory connectionFactory = new ConnectionFactory();
		connectionFactory.setHost("39.108.108.234");
		connectionFactory.setPort(5672);
		connectionFactory.setVirtualHost("/");
		
		Connection connection = connectionFactory.newConnection();
		Channel channel = connection.createChannel();
		
		
		String exchangeName = "test_consumer_exchange";
		String routingKey = "consumer.#";
		String queueName = "test_consumer_queue";
		
		channel.exchangeDeclare(exchangeName, "topic", true, false, null);
		channel.queueDeclare(queueName, true, false, false, null);
		channel.queueBind(queueName, exchangeName, routingKey);

		// 创建自定义消费者
		// QueueingConsumer queueingConsumer = new QueueingConsumer(channel);
		MyConsumer myConsumer = new MyConsumer(channel);

		/**
		 *	String queue, 队列名
		 *	boolean autoAck, 是否自动签收
		 *	Consumer callback 接收到消息的回调接口-是一个消费者对象，这里我们编写自定义消费者对象
		 */
		channel.basicConsume(queueName, true, myConsumer);
		
		
	}
}
